from sqlmodel import Field, Session, SQLModel, create_engine, select, text
from datetime import datetime
from functools import partial
from pytz import UTC


class BaseModel(SQLModel):
    """
    基础模型类，包含常用字段
    """

    id: int | None = Field(default=None, primary_key=True, description="主键 ID")
    created_by: str = Field(default="admin", description="创建者")
    updated_by: str = Field(default="admin", description="更新者")
    created_at: datetime = Field(
        default_factory=partial(datetime.now, UTC),
        description="创建时间",
        sa_column_kwargs={"server_default": text("CURRENT_TIMESTAMP")},
    )
    updated_at: datetime = Field(
        default_factory=partial(datetime.now, UTC),
        description="更新时间",
        sa_column_kwargs={
            "server_default": text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
        },
    )
    deleted: bool = Field(default=False, description="逻辑删除标志")
